import store from "@/store";

const disableClick = (event) => event && event.stopImmediatePropagation();

const Plugins = {
  install(Vue) {
    Vue.directive("loadMore", {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function () {
          const CONDITION =
            this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (CONDITION) {
            binding.value();
          }
        });
      },
    });

    Vue.directive("isNormal", {
      bind(el, binding) {
        // console.log(store.getters.roleId);
        const { roleId } = store.getters;
        judgeIsUser(el);
        switch (roleId) {
          case "0":
            // 超级管理员

            break;

          case "1":
            // 管理员
            break;
          case "2":
            // 普通用户
            break;
          default:
            break;
        }
      },
    });
  },
};

const judgeIsUser = (el) => {
  return false;
  el.addEventListener("click", disableClick, true); //禁止点击
  // 计数器
  const isInputNumber = el.classList.contains("el-input-number");
  if (isInputNumber) {
    el.setAttribute("disabled", "disabled");
    const inputNumber = el.querySelector("input");
    inputNumber.setAttribute("readonly", "readonly");
    inputNumber.setAttribute("disabled", "disabled");
    return;
  }

  // 开关
  const isSwitch = el.classList.contains("el-switch");
  if (isSwitch) {
    console.log(el);
    el.classList.add("is-disabled");
    el.setAttribute("disabled", "disabled");
    const switchInput = el.querySelector("input");
    switchInput.setAttribute("readonly", "readonly");
    switchInput.setAttribute("disabled", "disabled");

    // console.log(el);
    // console.log(el.classList);
    return;
  }

  // 多选框
  const isCheckbox = el.classList.contains("el-checkbox");
  if (isCheckbox) {
    isCheckbox.setAttribute("disabled", "disabled");
  }

  // status-icon
  const isStatusIcon = el.classList.contains("status-icon");
  if (isStatusIcon) {
    el.style.display = "none";
    return;
  }

  // 日期
  const isDateEditor = el.classList.contains("el-date-editor");
  if (isDateEditor) {
    const dateInput = el.querySelector("input");
    dateInput.setAttribute("readonly", "readonly");
  }

  // 按钮隐藏
  const isBtn = el.classList.contains("el-button");
  if (isBtn) {
    el.style.display = "none";
    return;
  }

  const select_dom = el.querySelector(".el-button");

  el.classList.add("is-disabled");
  if (select_dom) {
    select_dom.setAttribute("disabled", "disabled");
  }

  const select_dom2 = el.querySelector(".el-input");
  if (select_dom2) {
    select_dom2.classList.add("is-disabled");
  }
};

export default Plugins;
